package org.gl.utils;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

public class AndroidDrawableUtil {
	private Bitmap drawableToBitmap(Drawable drawable, int width, int height) {

		Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
				.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;
	}

	public void drawLayoutDropShadow(View view,int width, int height) {
		View linearLayout = view;
		BlurMaskFilter blurFilter = new BlurMaskFilter(3,
				BlurMaskFilter.Blur.OUTER);
		Paint shadowPaint = new Paint();
		shadowPaint.setMaskFilter(blurFilter);
		Drawable drawable = view.getBackground();
		int[] offsetXY = new int[2];
		Bitmap originalBitmap = drawableToBitmap(drawable, width, height);
		Bitmap shadowBitmap = originalBitmap
				.extractAlpha(shadowPaint, offsetXY);
		Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);

		Canvas c = new Canvas(shadowImage32);
		c.drawBitmap(originalBitmap, 0, 2, null);

		Drawable d = new BitmapDrawable(shadowImage32);
		linearLayout.setBackgroundDrawable(d);
	}
}
